/**
 * 
 */
package org.easy.system.manage.config;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import com.alibaba.druid.filter.Filter;
import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;

/**
 * @ClassName: MyBatisConfiguration
 * @Description: mybatis 配置类
 * @author: daiw
 * @date: 2019年1月25日 下午2:36:52
 * 
 * @Copyright: 2019 www.ccbao56.com Inc. All rights reserved.
 *             注意：本内容仅限于深圳旭珩科技内部传阅，禁止外泄以及用于其他的商业目
 */
@Configuration
@MapperScan(value = "org.easy.system.manage.dao")
public class MyBatisConfiguration {
	@Value("${spring.datasource.driver-class-name}")
	private String driverClassName;

	@Value("${spring.datasource.url}")
	private String url;

	@Value("${spring.datasource.username}")
	private String username;

	@Value("${spring.datasource.password}")
	private String password;

	@Value("${ccbao.druid.loginname}")
	private String druidLoginName;
	@Value("${ccbao.druid.password}")
	private String druidPassword;
	@Value("${mybatis.type-aliases-package}")
	private String typeAliasesPackage;

	@Bean
	public DruidDataSource dataSource() throws SQLException {
		DruidDataSource dataSource = new DruidDataSource();
		dataSource.setDriverClassName(this.driverClassName);
		dataSource.setUrl(this.url);
		dataSource.setUsername(this.username);
		dataSource.setPassword(this.password);

		dataSource.setInitialSize(5);
		dataSource.setMaxActive(200);
		dataSource.setMinIdle(5);
		dataSource.setMaxWait(60000L);
		dataSource.setTimeBetweenEvictionRunsMillis(60000L);
		dataSource.setFilters("mergeStat,log4j,wall");
		dataSource.setUseGlobalDataSourceStat(true);
		
		List<Filter> filters = new ArrayList<>();
		filters.add(statFilter());
		dataSource.setProxyFilters(filters);
		return dataSource;
	}

	@Bean(name = "statFilter")
	public StatFilter statFilter() {
		StatFilter statFilter = new StatFilter();
		statFilter.setSlowSqlMillis(3000L);
		statFilter.setLogSlowSql(true);
		return statFilter;
	}

	@Bean(name = "sqlSessionFactory")
	public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource) throws IOException {
		SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
		sqlSessionFactoryBean.setDataSource(dataSource);
		sqlSessionFactoryBean.setConfigLocation(new ClassPathResource("mybatis-config.xml"));
		sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/*.xml"));
		sqlSessionFactoryBean.setTypeAliasesPackage(typeAliasesPackage);
		return sqlSessionFactoryBean;
	}

	/**
	 * 注册DruidServlet
	 *
	 * @return
	 */
	@Bean
	public ServletRegistrationBean druidServletRegistrationBean() {
		ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean();
		servletRegistrationBean.setServlet(new StatViewServlet());
		servletRegistrationBean.addUrlMappings("/druid/*");
		//登录查看信息的账号密码.
		servletRegistrationBean.addInitParameter("loginUsername", druidLoginName);
		servletRegistrationBean.addInitParameter("loginPassword", druidPassword);
		return servletRegistrationBean;
	}

	/**
	 * 注册DruidFilter拦截
	 *
	 * @return
	 */
	@Bean
	public FilterRegistrationBean druidFilterRegistrationBean() {
		FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
		filterRegistrationBean.setFilter(new WebStatFilter());
		Map<String, String> initParams = new HashMap<String, String>();
		//设置忽略请求
		initParams.put("exclusions", "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*");
		filterRegistrationBean.setInitParameters(initParams);
		filterRegistrationBean.addUrlPatterns("/*");
		return filterRegistrationBean;
	}
}
